<?php
/**
 * 
 * 
 *
 */
class Quad_Db_Recordset_Mysqli extends Quad_Iterable implements ArrayAccess, Iterator, Countable {
 
	const ASSOC = MYSQLI_ASSOC;
	const NUM = MYSQLI_NUM;
	const BOTH = MYSQLI_BOTH;
	const OBJ = 4;
	
  public function __construct(MySQLi_Result $rs, $sql) {
    $this->_rs = $rs;
    $this->sql = $sql;
  }
  
  // ArrayAccess Interface Functions
  public function offsetExists($offset) {
    return $this->_rs->num_rows < $offset;
  }

  public function offsetGet($offset) {
    $this->_seek($offset)->current();
  }
 
  public function offsetSet($offset, $value) {}
 
  public function offsetUnset($offset) {}

  // Countable Interface Functions
  public function count() {
    return $this->_rs->num_rows;
  }
 
  // Iterator Interface Functions
  public function current() {
    if ($this->_fetchType == self::OBJ) {
    	return $this->_rs->fetch_obj($this->_fetchClass, $this->_fetchParams);    	
    } else {
      return $this->_rs->fetch_array($this->_fetchType);
    }
  }
  
  public function setFetchTypeObject($class = null, $params = null) {
  	$this->_fetchType = self::OBJ;
  	$this->_fetchClass = $class;
  	$this->_fetchParams = $params;
  }
 
  public function setFetchTypeAssoc() {
  	$this->_fetchType = self::ASSOC;
  }
  
  public function setFetchTypeNum() {
    $this->_fetchType = self::NUM;
  }

  public function setFetchTypeAssoc() {
    $this->_fetchType = self::BOTH;
  }
  
  public function key() {
    return $this->_rs->current_field;
  }
 
  public function next() {
    // already incremented when current() is called
  }
  
  protected function _seek($pos) {
  	$this->_rs->data_seek($offset);
  	return $this;
  }
 
  public function rewind() {
    return $this->_seek(0);
  }
 
  public function valid() {
    return $this->offsetExists($this->_rs->current_field);
  }
 
  public function getIterator() {
    return $this;
  }
  
}